package com.test.hello.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class FileController {

    @RequestMapping(value = "/file")
    public String upload(@RequestParam("file") MultipartFile[] files){
        String dir = "E:/uploadFile/";

        for (MultipartFile file:files){
            System.out.println("文件类型:"+file.getContentType());
            String filename = file.getOriginalFilename();
            String suffix = filename.substring(filename.length() - 3);
            System.out.println("文件名:"+filename);
            System.out.println("文件后缀:"+suffix);
            System.out.println("文件大小:"+file.getSize()/1024+"KB");

            File dirFile = new File(dir,filename);
            if (!dirFile.exists()){
                dirFile.mkdirs();
            }
            try {
                file.transferTo(dirFile);
                System.out.println("文件保存成功");
            }catch (IOException e){
                e.printStackTrace();
            }
        }

        return "OK";
    }

}
